package com.yidu.ladder.base.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yidu.ladder.base.dao.SubsetDao;
import com.yidu.ladder.base.domain.Subset;
import com.yidu.ladder.base.domain.SubsetVo;
import com.yidu.ladder.base.service.SubsetService;
import com.yidu.ladder.util.IDGenerator;

/**
 * 辅助档案业务层实现类
 * @author bamboo
 *
 */
@Service
public class SubsetServiceImpl implements SubsetService{
	@Autowired
	private SubsetDao subsetDao;
	/**
	 * 得到辅助档案dao
	 * @return 辅助档案dao
	 */
	public SubsetDao getSubsetDao() {
		return subsetDao;
	}
	/**
	 * 设置辅助档案dao
	 * @param subsetDao 辅助档案dao
	 */
	public void setSubsetDao(SubsetDao subsetDao) {
		this.subsetDao = subsetDao;
	}


	@Override
	public Map<String, Object> querySubsetWithPage(SubsetVo subsetVo) {
		//设置页面
		subsetVo.setPage((subsetVo.getPage() - 1) * subsetVo.getLimit());
		//调用dao层方法分页查询数据
		List<Subset> list = subsetDao.querySubsetWithPage(subsetVo);
		//拿到查询数据数
		int count = subsetDao.countSubset(subsetVo);
		//创建map对象返回前端需要的数据格式
		Map<String, Object> map = new HashMap<String, Object>();
		//map存放code
		map.put("code", 0);
		//map存放count
		map.put("count", count);
		//map存放data
		map.put("data", list);
		//返回这个map
		return map;
	}


	@Override
	public int countSubset(SubsetVo subsetVo) {
		//调用计数方法并返回
		return subsetDao.countSubset(subsetVo);
	}


	@Override
	public boolean saveSubset(Subset subset) {
		//为添加的辅助档案添加随机ID
		subset.setSubsetId(IDGenerator.getId());
		//添加的数据全为可用
		subset.setUseable("Y");
		//添加数据的当前时间
		subset.setCreateTime(new Date());
		//调用dao层的添加方法
		return subsetDao.addSubset(subset)== 1?true :false;
	}


	@Override
	public boolean updateSubset(Subset subset) {
		//调用修改方法并返回，如果为1就返回true，否则返回false
		return subsetDao.updateSubset(subset) == 1?true :false;
	}


	@Override
	public boolean frozenOrRecovesubset(String subsetId, String flag) {
		//创建传参的Map集合
		Map<String, Object> paramMap = new HashMap<String, Object>();
		//判断进行冻结还是还原
		//int status = flag;
		//根据逗号分割ID
		String[] subsetIds = subsetId.split(",");
		//定义执行结果返回的总数 因为成功返回1，所以把所有执行结果加起来正好就等于ID的总数
		int total = 0;
		//遍历subsetIds数组
		for (int i =0; i < subsetIds.length; i++) {
			//添加ID
			paramMap.put("subsetId", subsetIds[i]);
			//添加要修改的状态
			paramMap.put("useable", flag);
			//执行
			int result = subsetDao.frozenOrRecoveSubset(paramMap);
			//计数
			total += result;
		}
		//返回计数
		return total == subsetIds.length ? true : false;
	}
	
	@Override
	public Map<String, Object> findSubsetByArchivesId(String archivesId) {
		//调用dao层的查询方法
		List<Subset> list = subsetDao.findSubsetByArchivesId(archivesId);
		//创建map对象返回前端需要的数据格式
		Map<String, Object> map = new HashMap<String, Object>();
		//map存放code
		map.put("code", 0);
		//map存放data
		map.put("data", list);
		//返回这个map
		return map;
	}


	@Override
	public List<Subset> findTypeByArchives() {
		//查询辅助档案并返回
		return subsetDao.findSubsetBy();
	}
	

}
